{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 连续系统的时域分析\n",
    "\n",
    "![](chapter-2-overview.png)\n",
    "\n",
    "1. LTI（Linear Time-Invariant）系统的响应\n",
    "1. 冲激响应 $h(t)$\n",
    "1. 卷积积分"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 一、 LTI（Linear Time-Invariant）系统的响应\n",
    "\n",
    "#### 1. LTI系统的微分方程与框图表示\n",
    "\n",
    "LTI系统常使用 微分方程/差分方程 来描述，并可以绘制成对应的系统框图。\n",
    "\n",
    "系统框图可以形象地体现出系统的一些特性，而且是计算机仿真的基础，并且可以直接转换成程序。\n",
    "\n",
    "#### 基本部件\n",
    "\n",
    "1. 加法器\n",
    "1. 数乘器（放大器）\n",
    "1. 积分器（之所以不用微分器，是因为它实现困难，而且对误差和噪声特别灵敏）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 例题：已知系统的微分方程为 $y''(t) + 3y'(t) + 2y(t) = 4f'(t) + f(t)$，试画出它的系统框图。\n",
    "\n",
    "**方法一：积分方程法（简单低效）**\n",
    "\n",
    "两边直接二重积分，转化为积分方程，得 $y(t) = -3\\int \\! y(t)dt - 2\\iint \\! y(t)dt + 4\\int \\! f(t)dt + \\iint \\! f(t)dt$\n",
    "$y(t)$ 和 $f(t)$ 都有一二重积分，分别使用两个积分器，再用一个加法器便得框图了。\n",
    "\n",
    "**方法二：辅助函数法（高效实用）**\n",
    "\n",
    "先解对应的去掉 $f(t)$ 的导数项的辅助方程 $x''(t) + 3x'(t) + 2x(t) = f(t)$。这里 $x(t)$ 是能使方程成立的中间函数，不需要求它。\n",
    "\n",
    "辅助方程对应的变换为 $f(t) \\to x(t)$，由 LTI系统的特性有 $f'(t) \\to x'(t)$.\n",
    "\n",
    "现在若辅助方程的输入为 $4f'(t) + f(t)$, 由 LTI系统的特性，系统的响应应该为 $4x'(t) + x(t)$. 对比题目的方程，能发现这个响应就是 $y(t)$，即 $y(t) = 4x'(t) + x(t)$\n",
    "\n",
    "于是分别绘出$\n",
    "\\begin{cases}\n",
    "x''(t) + 3x'(t) + 2x(t) = f(t) \\\\\n",
    "y(t) = 4x'(t) + x(t)\n",
    "\\end{cases}\n",
    "$对应的框图，然后把式一输出的 $x'(t) \\text{和} x(t)$ 连接到式二的图上，就得到最终结果了。\n",
    "\n",
    "![](block-diagram-1.png)\n",
    "\n",
    "**方法三：微分算子法**\n",
    "\n",
    "该方法和辅助函数法类似，但是更简洁易懂。\n",
    "\n",
    "首先和方法二一样，按上图把系统分成两部分，利用中间变量 $x(t)$ 得到方程组：\n",
    "$$\n",
    "\\begin{cases}\n",
    "x''(t) + 3x'(t) + 2x(t) = f(t) \\\\\n",
    "y(t) = 4x'(t) + x(t)\n",
    "\\end{cases}\n",
    "$$\n",
    "然后利用微分算子 $D$ 表示微分操作 $d \\over dt$，$D^2$ 表示 $d^2 \\over dt^2$，可得\n",
    "$$\n",
    "\\begin{cases}\n",
    "(D^2 + 3D + 2)x(t) = f(t) \\\\\n",
    "y(t) = (4D + 1)x(t)\n",
    "\\end{cases}\n",
    "$$\n",
    "消去 $x(t)$ 得到\n",
    "$$\n",
    "\\frac{y(t)}{f(t)} = \\frac{4D + 1}{D^2 + 3D + 2}\n",
    "$$\n",
    "\n",
    "于是 $$y''(t) + 3y'(t) + 2y(t) = 4f'(t) + f(t)$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**从系统框图逆推微分方程**：\n",
    "\n",
    "先列出红线左边的框图对应的方程：$x''(t) = f(t) - 3x'(t) - 2x(t)$，移项得 $x''(t) - 3x'(t) - 2x(t) = f(t)$\n",
    "\n",
    "再列出右边的方程：$y(t) = 4x'(t) + x(t)$\n",
    "\n",
    "根据系统的 LTI 特性，当左端系统的输入（即方程左端的 $f(t)$）变成 $4f'(t) + f(t)$ 时，它的输入就是 $4x'(t) + x(t) = y(t)$. \n",
    "\n",
    "所以整个系统的方程为  $y''(t) + 3y'(t) + 2y(t) = 4f'(t) + f(t)$\n",
    "\n",
    "**也就是说在该系统的左端加个微分器，效果等同于在它右端加个加法器。**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. 微分方程的经典解法\n",
    "\n",
    "1. 二阶常系数齐次线性微分方程 $y''(t) + py'(t) + qy = f(x)$ 的解法：见高数\n",
    "\n",
    "**关于初值条件$t = 0_-\\ or\\ 0_+$ 的问题：**\n",
    "\n",
    "一般输入 $f(t)$ 都是在 $t=0$ 时接入系统的，为确定解的待定系数所需的是 $t=0_+$ 时 $y(t)$ 及其各阶导数的值，称作**初值**。（条件个数等于待定系数个数）\n",
    "\n",
    "而 $0_-$ 时刻，输入尚未接入，这个时候的值表示了系统的**初始状态**，**$0_+$ 的值（初值）可从 $0_-$ 的值和输入 $f(t)$ 推断得到。**\n",
    "\n",
    "1. 当 $f(t)$ 不包含冲激函数 $\\delta(t)$ 及其各阶导数时：**初值即初始状态**\n",
    "1. 否则，**利用系数匹配可推断出：等式右边 $\\delta(t)$ 的最高阶导数，应该是左边 $y(t)$ 的最高阶导数的组成部分。**\n",
    "\n",
    "    可用反证法证明，若 $\\delta(t)$ 的最高阶导数是 $y(t)$ 的低阶导数的组成部分，那么 $y(t)$ 的最高阶导数会包含 $\\delta(t)$ 的更高阶导数！矛盾！\n",
    "    \n",
    "    具体解法见下"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3. 求解LTI系统的零输入响应和零状态响应\n",
    "\n",
    "例：描述系统的微分方程为 $y''(t) + 3y'(t) + 2y(t) = 2f'(t) + 6f(t)$，初始状态 $y(0_-)=2,\\ y'(0_-)=0$，\n",
    "输入$f(t)=u(t)$，求系统的零输入响应和零状态响应。\n",
    "\n",
    "#### 3.1 零输入响应\n",
    "\n",
    "即输入为 0 时，仅受状态影响的响应。因为输入为 0，有 $f(t) = 0,\\ f'(t) = 0$，方程右端为0. \n",
    "可见**零输入响应 $y_{zi}(t)$ 即微分方程的齐次通解**。\n",
    "\n",
    "$y_{zi}''(t) + 3y_{zi}'(t) + 2y_{zi}(t) = 0$ 对应的特征方程为 $\\lambda^2 + 3\\lambda + 2 = 0$，根为 $\\lambda_1 = -1,\\ \\lambda_2 = -2$. 两个不等实根，所以通解为 $y_{zi}(t) = C_1 e^{-t} + C_2 e^{-2t}$.\n",
    "\n",
    "现在考虑初值条件：**输入为0，初始状态即初值条件**，代入解出未知系数，得到 $y_{zi}(t) = 4 e^{-t} - 2 e^{-2t}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.2 零状态响应\n",
    "\n",
    "**第一步：**\n",
    "\n",
    "状态为0，**即初始状态为 0**, 即 $y_{zs}(0_-)=0,\\ y_{zs}'(0_-)=0$. （对比很明显，求零输入响应时，排除掉实际输入 $f(t)=u(t)$；这里则排除掉实际初始状态 ）\n",
    "\n",
    "于是有 $\\begin{cases}\n",
    "y_{zs}''(t) + 3y_{zs}'(t) + 2y_{zs}(t) = 2\\delta(t) + 6u(t) \\\\\n",
    "y_{zs}(0_-)=0,\\ y_{zs}'(0_-)=0\n",
    "\\end{cases}$\n",
    "\n",
    "之前说过，**利用系数匹配可推断出：等式右边 $\\delta(t)$ 的最高阶导数，应该是左边 $y(t)$ 的最高阶导数的组成部分。**\n",
    "\n",
    "设 $y_{zs}''(t) = 2\\delta(t) + au(t) + r_0(t)$，就有\n",
    "\n",
    "$y_{zs}'(t) = 2u(t) + r_1(t)$\n",
    "\n",
    "$y_{zs}(t) =  r_2(t)$\n",
    "\n",
    "其中 $r_i(t)$ 为不含 $\\delta(t) \\ \\text{和} \\ u(t)$ 的项。代入方程可得 $a = 4$.\n",
    "\n",
    "因为 $y_{zs}(t) =  r_2(t)$，它在定义域上都是平滑的，所以有 $y_{zs}(0_+)=y_{zs}(0_-)=0$\n",
    "\n",
    "**第二步：**\n",
    "\n",
    "方程两边同时在 $[0_-, 0_+]$ 上积分，**所有不含 $\\delta(t)$的项，积分的结果都为 0**，于是得到\n",
    "\n",
    "$\\int_{0_-}^{0_+}y_{zs}''(t)\\ dt = 2\\int_{0_-}^{0_+}\\delta(t)\\ dt = 2$\n",
    "\n",
    "即 $y_{zs}'(0_+) - y_{zs}'(0_-) = 2$\n",
    "\n",
    "所以 $y_{zs}'(0_+)  = 2$。这样就把两个初始状态转化成了两个初值条件：\n",
    "\n",
    "$\\begin{cases}\n",
    "y_{zs}(0_+) = 0\\\\\n",
    "y_{zs}'(0_+)  = 2\n",
    "\\end{cases}$\n",
    "\n",
    "**第三步：**\n",
    "\n",
    "初值条件有了，现在要求得方程的全解，再代入初值条件就能求得零状态响应。\n",
    "\n",
    "**$t>0$ 时，$\\delta(t)=0,\\ u(t) = 1$**，方程变成了 $y_{zs}''(t) + 3y_{zs}'(t) + 2y_{zs}(t) = 6$，现在用经典方法求解。\n",
    "\n",
    "齐次通解为 $y_{zsh}(t) = C_1e^{-t} + C_2e^{-2}t$，\n",
    "\n",
    "非齐次特解为 $y_{zsp}(t) = 3$\n",
    "\n",
    "使用两个初值条件，解出两个系数，**得到结果为 $y_{zs}(t) = -4e^{-t} + e^{-2t} + 3,\\ t > 0$**, 也可利用阶跃函数改写成 $y_{zs}(t) = (-4e^{-t} + e^{-2t} + 3)\\ u(t)$.\n",
    "\n",
    "**NOTE: 零状态响应不等同于方程的特解！**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4. 响应的分类\n",
    "\n",
    "![](response-type.png)\n",
    "\n",
    "#### 4.1 固有响应和强迫响应\n",
    "\n",
    "**固有响应**：即与输入无关，只与系统本身的特性有关的响应。对应方程的「齐次通解」\n",
    "\n",
    "**强迫响应**: 即受激励函数影响的那一部分响应，**对应方程的「非齐次特解」**（相当于零状态响应的一部分）\n",
    "\n",
    "#### 4.2 暂态响应和稳态响应\n",
    "\n",
    "**暂态响应**：即「全响应」中，会快速衰减的那一部分，$\\lim_{t \\to \\infty} = 0$。典型的有 $e^{-\\alpha t}$, 它会随着时间衰减，因此其影响是暂时的。\n",
    "\n",
    "**稳态响应**：即「全响应」-「暂态响应」，当时间足够长时，「暂态响应」会完全消失，系统达到稳态，此时的输出即为「稳态响应」。"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 64-bit",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
